package com.seari.controller;

import com.seari.pojo.DevCheckRepairPlanPojo;
import com.seari.pojo.MessageBean;
import com.seari.service.DevCheckRepairService;
import com.seari.service.UserService;
import com.seari.utils.ParseUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.List;


/**
 *
 * 设备分类
 */
@Controller
@RequestMapping("dev/check/repair")
public class DevCheckRepairController {

    @Autowired
    private DevCheckRepairService devCheckRepairService;
    @Autowired
    private UserService userService;

    /**
     * 新增检修记录
     * @return
     */
    @RequestMapping("addOrUpdate")
    @ResponseBody
    public MessageBean addOrUpdate(HttpServletRequest request,DevCheckRepairPlanPojo checkRepairPojo){
        MessageBean message = new MessageBean("0",null,"成功");
        try{
            checkRepairPojo.setUserName(ParseUtils.getUidFromToken(request,userService));
            /*Claims claim = JwtUtil.parseJWT(token);
            String stationName = claim.get("stationName")+"";
            QueryUser qu = new QueryUser();
            qu.setStationName(stationName);
            qu.setUserName(claim.getSubject());
            List<User> userList = userService.getUserByParams(qu);
            if(userList!=null&&userList.size()>0){
                checkRepairPojo.setUserName(userList.get(0).getUid()+"");
            }*/
            devCheckRepairService.addOrUpdateCheckRepairRecord(checkRepairPojo);
        }catch (Exception e){
            message.setCode("2");
        }
        return message;
    }

    /**
     * 删除检修记录
     * @param taskNo
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    MessageBean delete(String taskNo, String split){
        MessageBean message = new MessageBean("0",null,"成功");
        String[] taskNoArr = taskNo.split(split);
        //devCheckRepairService.deleteCheckRepairRecord(taskNo);
        devCheckRepairService.deleteCheckRepairByNos(taskNoArr);
        return message;
    }

    /**
     * 检修记录列表
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public MessageBean list(){
        MessageBean message = new MessageBean("0",null,"成功");
        List<DevCheckRepairPlanPojo> list = devCheckRepairService.getCheckRepairRecordList();
        message.setData(list);
        return message;
    }
}
